package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysConfig;
import cn.wgx.modules.sys.util.SysConfigUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "10. 系统设置")
@RestController
@RequestMapping(value="${allPathPre}sysConfig")
public class SysConfigController extends BaseController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisTemplate redisTemplate;

    static String cacheKey = "SYS_CONFIG_123";

    @ApiOperation("获取公开的配置")
    @RequestMapping(path = "listPublic", method = RequestMethod.GET)
    public Map<String,Object> getSysPublicConfig() {
        SysConfig.Config configByClass = SysConfigUtil.getConfigByClass(SysConfig.Config.class);
        return returnData(configByClass);
    }

    @ApiOperation("修改公开的配置")
    @ApiPermission
    @RequestMapping(path = "updateConfig", method = RequestMethod.PATCH)
    public Map<String,Object> updateSysConfig(@RequestBody SysConfig.Config config) {
        return returnData(SysConfigUtil.updateConfig(config));
    }

    @ApiOperation("获取全部配置")
    @ApiPermission
    @RequestMapping(path = "list", method = RequestMethod.GET)
    public Map<String,Object> getList() {
        return returnData(SysConfigUtil.getAllConfig());
    }



    @ApiOperation("修改配置 - rtmp")
    @ApiPermission
    @RequestMapping(path = "updateRtmpConfig", method = RequestMethod.PATCH)
    public Map<String,Object> updateRtmpConfig(@RequestBody SysConfig.RtmpConfig config) {
        if(config == null){
            return returnData(0);
        }
        return returnData(SysConfigUtil.updateConfig(config));
    }


    @ApiOperation("修改配置 - fastdfs")
    @ApiPermission
    @RequestMapping(path = "updateFastfdsConfig", method = RequestMethod.PATCH)
    public Map<String,Object> updateFastConfig(@RequestBody SysConfig.FastdfsConfig config) {
        if(config == null){
            return returnData(0);
        }
        return returnData(SysConfigUtil.updateConfig(config));
    }



}
